package com.test.drawing;

import javax.microedition.khronos.opengles.GL10;

/**
 * OpenGLCamera.<br>
 * Position the User Projection in the OpenGL.
 */
public class OpenGLCamera {

	// private static float[] position = new float[] { 20, 50, 70 };
	// private static float[] position = new float[] { 15/2*-1, 50, -100 };
	private static float[] position = new float[] { 0, 0, 0 };
	private static float[] eyePoint = new float[] { 0, 0, -30 };

	private static final int AXIS_X = 0;
	private static final int AXIS_Y = 1;
	private static final int AXIS_Z = 2;
	
	private static int angleToRotate = 0;
	public static float angle = 0f;

	/**
	 * Gets Current Camera Position.
	 * 
	 * @return {x , y, z}
	 */
	public static float[] getPosition() {
		return position;
	}

	/**
	 * Sets Current Camera Position.
	 * 
	 * @param x
	 *            new x position
	 * @param y
	 *            new y position
	 * @param z
	 *            new z position
	 */
	public static void setPosition(float x, float y, float z) {
		position = new float[] { x, y, z };
	}

	/**
	 * Position the Camera in the OpenGL Interface
	 * 
	 * @param gl
	 *            OpenGL Interface
	 */
	public static void positionCamera(GL10 gl) {

		// Camera Position an EyePoint

		// Little Far Camera
		// GLU.gluLookAt(gl, 0, 10, 0, 0, 0, -15, 0, 1, 0);

		// Big Animation Camera
		// GLU.gluLookAt(gl, 0, 0, 600, 0, 0, 0, 0, 1, 0);

		// Stadium Camera
		// GLU.gluLookAt(gl, 80, 70, 50, 0, 0, 0, 0, 1, 0);

		// Moving Camera
		
//		gl.glRotatef(angle, 0, 1, 0);

//		GLU.gluLookAt(gl, position[AXIS_X], position[AXIS_Y], position[AXIS_Z],
//				eyePoint[AXIS_X], eyePoint[AXIS_Y], eyePoint[AXIS_Z], 0, 1, 0);
		
//		gl.glTranslatef(-position[AXIS_X], -position[AXIS_Y], -position[AXIS_Z]);
	}
	
	public static void incrementAngle() {
		angleToRotate++;
	}
	
	public static void decrementAngle() {
		angleToRotate--;
	}
	

	/**
	 * Rotate Camera Over X Axis using the Given Angle
	 * 
	 * @param angle
	 *            Angles to Rotate
	 */
	public static void rotateOverX(float angle) {
		rotOver(AXIS_X, angle);
	}

	/**
	 * Rotate Camera Over Y Axis using the Given Angle
	 * 
	 * @param angle
	 *            Angles to Rotate
	 */
	public static void rotateOverY(float angle) {
		rotOver(AXIS_Y, angle);
	}

	/**
	 * Rotate Camera Over Z Axis using the Given Angle
	 * 
	 * @param angle
	 *            Angles to Rotate
	 */
	public static void rotateOverZ(float angle) {
		rotOver(AXIS_Z, angle);
	}
	
	/*
	 * Rotates the Camera
	 */
	private static void rotOver(int axisToRotate, float angle) {

		int axis1 = 0, axis2 = 0;

		if (axisToRotate == AXIS_X) {

			axis1 = AXIS_Y;
			axis2 = AXIS_Z;

		} else if (axisToRotate == AXIS_Y) {

			axis1 = AXIS_X;
			axis2 = AXIS_Z;

		} else if (axisToRotate == AXIS_Z) {

			axis1 = AXIS_X;
			axis2 = AXIS_Y;

		} else {
			// Invalid Axis
		}

		// Converts Origin to Position
		float x = eyePoint[axis1] - position[axis1], y = eyePoint[axis2]
				- position[axis2];

		// Rotate
		eyePoint[axis1] = (float) (x * Math.cos(Math.toRadians(angle)) - y
				* Math.sin(Math.toRadians(angle)));

		eyePoint[axis2] = (float) (x * Math.sin(Math.toRadians(angle)) + y
				* Math.cos(Math.toRadians(angle)));

		// Back to original Origin
		eyePoint[axis1] += position[axis1];
		eyePoint[axis2] += position[axis2];
	}

	/**
	 * Sets the Position of the Camera from the X Axis.
	 * 
	 * @param eyeX
	 *            X Position
	 */
	public static void setEyeXPosition(float eyeX) {
		position[AXIS_X] = eyeX;
	}

	/**
	 * Sets the Position of the Camera from the Y Axis.
	 * 
	 * @param eyeY
	 *            Y Position
	 */
	public static void setEyeYPosition(float eyeY) {
		position[AXIS_Y] = eyeY;
	}

	/**
	 * Sets the Position of the Camera from the Z Axis.
	 * 
	 * @param eyeZ
	 *            Z Position
	 */
	public static void setEyeZPosition(float eyeZ) {
		position[AXIS_Z] = eyeZ;
	}

	/**
	 * Get the Current Position of the Camera from the X Axis.
	 * 
	 * @return X Axis Position
	 */
	public static float getEyeXPosition() {
		return position[AXIS_X];
	}

	/**
	 * Get the Current Position of the Camera from the Y Axis.
	 * 
	 * @return Y Axis Position
	 */
	public static float getEyeYPosition() {
		return position[AXIS_Y];
	}

	/**
	 * Get the Current Position of the Camera from the Z Axis.
	 * 
	 * @return Z Axis Position
	 */
	public static float getEyeZPosition() {
		return position[AXIS_Z];
	}
}
